package com.example.booklist.service;

import com.example.booklist.mapper.BookInfoMapper;
import com.example.booklist.model.BookInfo;
import com.example.booklist.model.PageRequest;
import com.example.booklist.model.PageResult;
import com.example.booklist.enums.BookStatus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * ClassName: BookService
 * Package: IntelliJ IDEA
 * Description:
 *
 * @Author 谷京京
 * @Create 2023/10/30 18:35
 * @Version 1.0
 */
@Service
@Slf4j
public class BookService {
    @Autowired
    private BookInfoMapper bookInfoMapper;

    public PageResult<BookInfo> selectBookInfoByPage(PageRequest pageRequest) {
        if(pageRequest == null) {
            return null;
        }
        Integer count = bookInfoMapper.count();
        List<BookInfo> bookInfoList = bookInfoMapper.selectBookInfoByPage(pageRequest.getOffset(), pageRequest.getPageSize());
        if(bookInfoList != null && bookInfoList.size() > 0) {
            for (BookInfo book :
                    bookInfoList) {
                book.setStatusCN(BookStatus.getNameByCode(book.getStatus()).getName());
            }
        }
        return new PageResult<>(bookInfoList, count, pageRequest);
    }
    public Integer addBook(BookInfo bookInfo) {
        Integer ans = 0;
        try {
            ans = bookInfoMapper.insertBook(bookInfo);

        } catch (Exception e) {
            log.error("添加图书出错, e:{}", e);
        }
        return ans;
    }
    public BookInfo queryBookInfoById(Integer id) {
         return bookInfoMapper.queryBookInfoById(id);
    }
    public Integer updateBook(BookInfo bookInfo) {
        Integer result = 0;
        try {
            result = bookInfoMapper.updateBook(bookInfo);
        } catch (Exception e) {
            log.error("更新图书失败, e:{}", e);
        }
        return result;
    }
    public Integer batchDelete(List<Integer> ids) {
        Integer result = 0;
        try {
            result = bookInfoMapper.batchDelete(ids);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("删除失败");
        }
        return result;
    }
}
